import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/cupertino.dart' hide Action;
import 'package:flutter/material.dart' hide Action;
import 'package:flutter/widgets.dart' hide Action;
import 'package:fluwx/fluwx.dart' as fluwx;
import 'package:permission_handler/permission_handler.dart';
import 'package:www.znpigai.teacher/global_store/state.dart';
import 'package:www.znpigai.teacher/utils/nav.dart';
import 'package:www.znpigai.teacher/utils/util.dart';

import 'action.dart';
import 'state.dart';

Effect<CameraState> buildEffect() {
  return combineEffects(<Object, Effect<CameraState>>{
    CameraAction.onGuide: _onGuide,
    CameraAction.onShare: _onShare,
    CameraAction.onCamera: _onCamera,
    CameraAction.onOpenPracticeList: _onPracticeList,
    CameraAction.onOpenPracticeVoice: _onPracticeVocie,
  });
}

void _onGuide(Action action, Context<CameraState> ctx) async {
  Navigator.of(ctx.context).pushNamed(Nav.PRACTICE_GUIDE, arguments: null);
}

void _onCamera(Action action, Context<CameraState> ctx) async {
  const permissionGroups = [
    PermissionGroup.camera,
    PermissionGroup.photos,
    PermissionGroup.storage
  ];

  Map<PermissionGroup, PermissionStatus> permissions =
      await PermissionHandler().requestPermissions(permissionGroups);

  var denies = [];
  permissions.forEach((key, value) {
    if (value == PermissionStatus.denied) {
      denies.add(key);
    }
  });

  if (denies.isEmpty) {
    Navigator.of(ctx.context).pushNamed(Nav.PRACTICE_CAMERA, arguments: null);
  } else {
    var msg = denies.join("\n");
    Util.showMsgDialog("权限", "$msg\n以上权限被禁止，无法继续使用", ctx.context);
  }
}

void _onShare(Action action, Context<CameraState> ctx) async {
  final String select = await Util.showShareDialog(ctx.context);

  var title = GlobalState.isTeacher ? '作文批改教师版' : '作文批改学生版';
  var desc = "AI老师给你作文意想不到的精批细改！";
  var url = GlobalState.baseUrl("/correct/pub/anyone/view/app/share.mvc?t=0");
  var image = "assets://assets/images/ic_launcher.png";

  if (select == 'session') {
    fluwx.share(fluwx.WeChatShareWebPageModel(
        title: title,
        description: desc,
        scene: fluwx.WeChatScene.SESSION,
        webPage: url,
        thumbnail: image));
  } else if (select == 'timeline') {
    fluwx.share(fluwx.WeChatShareWebPageModel(
        title: title,
        description: desc,
        scene: fluwx.WeChatScene.TIMELINE,
        webPage: url,
        thumbnail: image));
  }
}

void _onPracticeList(Action action, Context<CameraState> ctx) {
  Navigator.of(ctx.context).pushNamed(Nav.PRACTICE_LIST);
}

void _onPracticeVocie(Action action, Context<CameraState> ctx) async {
  const permissionGroups = [
    PermissionGroup.microphone,
  ];

  Map<PermissionGroup, PermissionStatus> permissions =
      await PermissionHandler().requestPermissions(permissionGroups);

  var denies = [];
  permissions.forEach((key, value) {
    if (value == PermissionStatus.denied) {
      denies.add(key);
    }
  });

  if (denies.isEmpty) {
    Navigator.of(ctx.context).pushNamed(Nav.PRACTICE_VOICE);
  } else {
    var msg = denies.join("\n");
    Util.showMsgDialog("权限", "$msg\n以上权限被禁止，无法继续使用", ctx.context);
  }
}
